import time
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait

from base.base_page import BasePage


class SearchPage(BasePage):
    _search_edit = (AppiumBy.CLASS_NAME, "android.widget.EditText")
    _search_button = (AppiumBy.XPATH,
                      "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[3]")
    _response_text = (AppiumBy.XPATH,
                      "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View/android.view.View[1]/android.view.View[1]")

    def search(self, keyword):
        """
        element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId"))
        is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
        """
        # 隐式等待
        self.driver.implicitly_wait(3)
        # 显式等待
        # WebDriverWait(self.driver, 10).until(lambda x: x.find_element(self._search_edit))
        self.find_element_and_click(self._search_edit)
        # 点击搜索框
        # self.driver.find_element(by=AppiumBy.CLASS_NAME, value="android.widget.EditText").click()
        # 录入搜索内容
        # self.driver.find_element(by=AppiumBy.CLASS_NAME, value="android.widget.EditText").send_keys(keyword)
        self.find_element(self._search_edit).send_keys(keyword)
        # 点击搜索框确认内容，并显示搜索按钮
        # self.driver.find_element(by=AppiumBy.CLASS_NAME, value="android.widget.EditText").click()
        self.find_element_and_click(self._search_edit)
        # 点击搜索按键
        time.sleep(2)
        # self.driver.find_element(by=AppiumBy.XPATH,
        #                          value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[3]").click()

        self.find_element_and_click(self._search_button)

        return self

    def get_current_result(self):
        # return self.driver.find_element(by=AppiumBy.XPATH,
        #                                 value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View/android.view.View[1]/android.view.View[1]").text

        return self.find_element(self._response_text).text
